package org.example.spring_annotation.controller.api;

import io.swagger.annotations.*;
import org.example.spring_annotation.dto.ApiResponseData;
import org.example.spring_annotation.po.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api
@RequestMapping("/user")
@RestController
public class UserController {

    @PostMapping("/login")
    @ApiOperation(
            value = "登录", httpMethod = "POST", notes = "账号登录",
            response = User.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "username", value = "账号", dataType = "String"),
            @ApiImplicitParam(required = true, name = "password", value = "密码", dataType = "String")
    })
    public ApiResponseData<User> login(String username, String password) {
        ApiResponseData<User> data = new ApiResponseData<>();
        if (null == username || null == password) {
            data.setCode(1);
            data.setMessage("用户名和密码不能为空");
        } else if ("admin".equals(username) && "admin".equals(password)) {
            data.setData(new User(1, "张三", username, password));
        } else {
            data.setCode(2);
            data.setMessage("账号或密码错误");
        }
        return data;
    }
}
